﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Management;
using System.Threading;
using Microsoft.Win32;

namespace FEPClientService
{
    public partial class ClientService : ServiceBase
    {
        private string _HostURL = "";
        private int _PollingInterval = 0;
        private bool _ServiceStopping=false;
        private Thread _ServiceThread;
        private FEP.FepCom.CommandPack _CurrentCommand;
        private bool _LastActionSucceeded=false;
        private FEP.FepCom.ActionPack _LastAction = new FEP.FepCom.ActionPack();
        private string _LastActionResponseData = "";
        private bool _LastActionRunning=false;
        private Thread _ActionThread;
        FEP.RegistryHelper _rh = new FEP.RegistryHelper();
        FEP.Helper _helper = new FEP.Helper();
        FEP.EventHelper _eh = new FEP.EventHelper("FepClientService");
        FEP.FepCom.MGRService _MGRService;
        public ClientService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            
            Debugger.Launch();
            ServiceInit();
            ThreadStart ts = new ThreadStart(MainProcess);
            _ServiceThread = new Thread(ts);
            _ServiceThread.Start();
            
        }
        protected void MainProcess()
        {
            int i = 0;
            while (i == 0)
            {


                //Delay thread execution
                Thread.Sleep(_PollingInterval);
                _CurrentCommand = _MGRService.Poll(buildStatus("\\\\."));
                //IF command has been issued to service, start the Action Thread
                if (_CurrentCommand.Action.TypeOfAction != FEP.FepCom.ActionType.Nothing)
                {
                    if (_LastActionRunning)
                        break;
                    _LastActionSucceeded = false;
                    _LastActionRunning = true;


                    ThreadStart ts = new ThreadStart(ActionProcess);
                    _ActionThread = new Thread(ts);
                    _ActionThread.Start();
                }
                //Break loop if service stopping
                if (_ServiceStopping == true)
                {
                    i = 1;
                    break;
                }
            }
        }
        protected void ActionProcess()
        {
            _LastActionRunning = true;
            string results = "";
            try
            {
                _LastAction = _CurrentCommand.Action;
                
                _LastActionSucceeded = true;
            }
            catch { _LastActionSucceeded = false; }

            _LastActionResponseData = results;
            _LastActionRunning = false;
        }
        public FEP.FepCom.StatusPack buildStatus(string Computer)
        {
            FEP.FepCom.StatusPack retval = new FEP.FepCom.StatusPack(Computer);
            retval.LastActionSucceeded = _LastActionSucceeded;
            retval.LastAction = _LastAction;
            retval.ActionResponseDataXML = _LastActionResponseData;
            retval.ActionRunning = _LastActionRunning;

            return retval;
        }
        protected override void OnStop()
        {
            _ServiceStopping = true;
            System.Threading.Thread.Sleep(_PollingInterval + 5000);
        }
        protected void ServiceInit()
        {
            _HostURL = _rh.HostURL;
            _MGRService = new FEP.FepCom.MGRService(_HostURL);
            _eh.writeEvent(EventLogEntryType.Information, 1000, "FEPClientService Started with Host: " + _HostURL);
            _LastAction = new FEP.FepCom.ActionPack();
        }
    }
}
